//
//  ImagePanelGroupTableViewCell.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15-6-3.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "ImagePanelGroupTableViewCell.h"

#import "ImagePanel.h"

@interface ImagePanelGroupTableViewCell ()<ImagePanelDelegate>
{
    ImagePanel *_imagePanel;
    
    NSMutableArray *_selectedImagePath;
}
@end

@implementation ImagePanelGroupTableViewCell

@synthesize selectedImagePath = _selectedImagePath;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
   
    _selectedImagePath = [NSMutableArray new];
    
    _imagePanel = [[ImagePanel alloc]initWithFrame:CGRectMake(0, 5, self.width, 70)];
    _imagePanel.delegate = self;
    [self.contentView addSubview:_imagePanel];
    
    return self;

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _imagePanel.size = CGSizeMake(self.width, 70);
}

-(void)addImage:(UIImage *)image withTag:(NSString *)tag
{
    [_selectedImagePath addObject:tag];
    
    [_imagePanel addImage:image withTag:tag];
}

-(void)replaceImage:(UIImage*)image atTag:(NSString*)tag
{
    [_imagePanel replaceImage:image withTag:tag];
}

-(void)clickAddImageButtonOfImagePanel:(ImagePanel*)imagePanel
{
    if([self.delegate respondsToSelector:@selector(clickAddImageButtonOfImagePanelGroupTableViewCell:)])
            [(id<ImagePanelGroupTableViewCellDelegate>)self.delegate clickAddImageButtonOfImagePanelGroupTableViewCell:self];
}

-(void)clickDeleteImageButtonOfImagePanel:(ImagePanel*)imagePanel
{
    if([self.delegate respondsToSelector:@selector(clickDeleteImageButtonOfImagePanelGroupTableViewCell:)])
        [(id<ImagePanelGroupTableViewCellDelegate>)self.delegate clickDeleteImageButtonOfImagePanelGroupTableViewCell:self];
}

-(void)clickDeleteModeImageButtonOfImagePanel:(ImagePanel*)imagePanel atTag:(NSString*)tag
{
    [_selectedImagePath removeObject:tag];
    
    [_imagePanel deleteImageWithTag:tag];
    
    if([self.delegate respondsToSelector:@selector(clickDeleteModeImageButtonOfImagePanelGroupTableViewCell: atTag:)])
        [(id<ImagePanelGroupTableViewCellDelegate>)self.delegate clickDeleteModeImageButtonOfImagePanelGroupTableViewCell:self atTag:tag];
}

+ (CGFloat)caleHeight
{
    return 80;
}

@end

@interface ImagePanelStaticGroupTableViewCell ()<ImagePanelDelegate>
{
    ImagePanel *_imagePanel;
    
    UILabel *_titleLabel;
    
    NSMutableArray *_imageFiles;
}
@end

@implementation ImagePanelStaticGroupTableViewCell

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _imageFiles = [NSMutableArray new];
    
    _imagePanel = [[ImagePanel alloc]initWithFrame:CGRectMake(0, 5, self.width, 70)];
    _imagePanel.delegate = self;
    _imagePanel.controlEnable = NO;
    [self.contentView addSubview:_imagePanel];
    
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    if(_titleLabel != nil)
    {
        _titleLabel.left = 10;
        _titleLabel.centerY = self.contentView.height / 2;
        
        _imagePanel.left = _titleLabel.right + 10;
        _imagePanel.size = CGSizeMake(self.contentView.width - _titleLabel.right + 10, 70);
    }
    else
    {
        _imagePanel.size = CGSizeMake(self.contentView.width, 70);
    }
}

-(void)setTitle:(NSString *)title
{
    if(title.length > 0)
    {
        if(_titleLabel == nil)
        {
            CGSize size = [NSString charSizeWithSystemFontSize:13];
            _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size.width * 6, size.height)];
            _titleLabel.backgroundColor = [UIColor clearColor];
            _titleLabel.textColor = [UIColor grayColor];
            _titleLabel.font = [UIFont systemFontOfSize:13];
            _titleLabel.textAlignment = NSTextAlignmentRight;
            [self.contentView addSubview:_titleLabel];
        }
        _titleLabel.text = title;
    }
    else
    {
        [_titleLabel removeFromSuperview];
        _titleLabel = nil;
    }
}

-(void)addImage:(UIImage *)image withTag:(NSString *)tag
{
    [_imageFiles addObject:tag];
    
    [_imagePanel addImage:image withTag:tag];
}

-(void)replaceImage:(UIImage*)image atTag:(NSString*)tag
{
    [_imagePanel replaceImage:image withTag:tag];
}

-(void)imagePanel:(ImagePanel *)imagePanel didClickedImageButton:(ImageButton *)imageButton imageContains:(NSArray *)imageContains
{
    if([self.delegate respondsToSelector:@selector(imagePanelStaticGroupTableViewCell: clickedImageButtonTag: imageContain:)])
    [(id<ImagePanelStaticGroupTableViewCelldelegate>)self.delegate imagePanelStaticGroupTableViewCell:self clickedImageButtonTag:imageButton.szTag imageContain:imageContains];
}

+ (CGFloat)caleHeight
{
    return 80;
}

-(void)setImageNames:(NSArray *)imageNames
{
    for (NSString *item in imageNames)
    {
        UIImage *image = [UIImage imageWithContentsOfFile:item];
        [self addImage:image withTag:item];
    }
}

-(NSArray*)imageNames
{
    return _imageFiles;
}

@end
